package com.auto.survey.activity.config

import com.auto.survey.activity.config.dao.ConfigDao
import com.auto.survey.activity.config.entity.ConfigBean
import com.auto.survey.activity.config.entity.PointConfig

class ConfigHelpr {

    private var currentConfigId: Int? = null  // 新增：暂存 configId
    private val pointList: MutableList<PointConfig> = mutableListOf()

    // 创建新的 ConfigBean 并获取其 id
    suspend fun createNewConfig(pc_data: Double, tmp_data: Double,  hum_data: Double, kap_data: Double,
        higt_data: Double, stnum_data: Int, configDao: ConfigDao ): Long? {
        val config = ConfigBean(
            pc_data = pc_data,
            tmp_data = tmp_data,
            hum_data = hum_data,
            kap_data = kap_data,
            higt_data = higt_data,
            stnum_data = stnum_data
        )
        // 插入 ConfigBean 并获取 id
        val configId = configDao.insertConfig(config)
        currentConfigId = configId.toInt()  // 保存 configId
        pointList.clear()
        return configId
    }

    // 动态添加 PointConfig（现在包含 configId）
    fun addPointConfig( point_x_data: Double, point_y_data: Double, point_z_data: Double,
        point_mirr_h: Double, point_mirr_num: Long, point_name: String ): PointConfig {
        // 检查 configId 是否已设置
        val configId = currentConfigId ?: throw IllegalStateException("必须先创建 ConfigBean")

        val point = PointConfig(
            configId = configId,  // 现在设置 configId
            point_x_data = point_x_data,
            point_y_data = point_y_data,
            point_z_data = point_z_data,
            point_mirr_h = point_mirr_h,
            point_mirr_num = point_mirr_num,
            point_name = point_name
        )
        pointList.add(point)
        return point
    }
}